package com.design.create.singleton;

import java.util.HashMap;
import java.util.Map;

/**
 * 登记式单例
 */
public class RegisterSingleton {

    private static Map<String,RegisterSingleton> map = new HashMap<>();

    static {
        RegisterSingleton registerSingleton = new RegisterSingleton();
        map.put(registerSingleton.getClass().getName(),registerSingleton);
    }

    protected RegisterSingleton() {}

    public static RegisterSingleton getInstance(String name) {
        if (name == null) {
            name = RegisterSingleton.class.getName();
            //打印全限定名
            System.out.println("name == null" + "--------->name="+ name);
        }
        if (map.get(name) == null) {
            try {
                map.put(name,(RegisterSingleton)Class.forName(name).newInstance());
            } catch (Exception exception) {
                exception.printStackTrace();;
            }
        }
        return map.get(name);
    }

    public String about() {
        return "Hello I am RegisterSingleton";
    }

    public static void main(String[] args) {
        RegisterSingleton registerSingleton = RegisterSingleton.getInstance(null);
        System.out.println(registerSingleton.about());
    }
}
